package view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import view.EmployeesPanel.TableModel;

import model.Employee;
import net.miginfocom.swing.MigLayout;

public class EmployeeDialog extends JDialog{
	private EmployeesPanel parent;
	private Employee emp;
	
	private JTextField namefield;
	private JTextField emailfield;
	private JTextField ssnfield;
	private JTextField dobfield;
	private JTextField addressfield;
	private JTextField phoneNumberfield;
	private JComboBox departmentbox;
	private JComboBox genderbox;
	private JTextField emergencyContactfield;
	private JTextField titlefield;
	private JTextField salaryfield;
	
	public EmployeeDialog(EmployeesPanel parent, Employee e){
		this.parent = parent;
		this.emp = e;
		this.setTitle("Employee Information");
		this.setSize(new Dimension(300,300));
		initGUI();
		
		if(emp.getName() != null){
			loadEmployeeInfo();
		}
		
	}
	
	private void loadEmployeeInfo(){
		namefield.setText(emp.getName());
		emailfield.setText(emp.getEmail());
		ssnfield.setText(emp.getSSN());
		dobfield.setText(emp.getDateOfBirth());
		addressfield.setText(emp.getAddress());
		phoneNumberfield.setText(emp.getPhoneNumber());
		emergencyContactfield.setText(emp.getEmergencyContact());
		titlefield.setText(emp.getTitle());
		salaryfield.setText(Float.toString(emp.getSalary()));
		departmentbox.setSelectedItem(emp.getDepartment());
		genderbox.setSelectedItem(emp.getGender());
	}
	
	private void initGUI(){
		this.setLayout(new MigLayout("fillx,insets 25","[][][]30[][][]"));
		
		namefield = new JTextField(16);
		emailfield = new JTextField(16);
		ssnfield = new JTextField(16);
		dobfield = new JTextField(16);
		addressfield = new JTextField(16);
		phoneNumberfield = new JTextField(16);
		emergencyContactfield = new JTextField(16);
		titlefield = new JTextField(16);
		salaryfield = new JTextField(16);
		departmentbox = new JComboBox(new String[]{"Reception","Room Service","House Keeping", "Kitchen", "Mantainence","Security"});
		genderbox = new JComboBox(new String[]{"Male","Female"});
		
		this.add(new JLabel("Name: "),"cell 1 1");
		this.add(namefield,"cell 2 1");
		this.add(new JLabel("Email: "),"cell 4 1");
		this.add(emailfield,"cell 5 1");
		this.add(new JLabel("Address: "),"cell 1 2");
		this.add(addressfield,"cell 2 2");
		this.add(new JLabel("D.O.B."), "cell 4 2");
		this.add(dobfield,"cell 5 2");
		this.add(new JLabel("Phone: "),"cell 1 3");
		this.add(phoneNumberfield,"cell 2 3");
		this.add(new JLabel("SSN"),"cell 4 3");
		this.add(ssnfield, "cell 5 3");
		this.add(new JLabel("Emergency Contact: "),"cell 1 4");
		this.add(emergencyContactfield,"cell 2 4");
		this.add(new JLabel("Gender: "),"cell 4 4");
		this.add(genderbox,"cell 5 4");
		this.add(new JLabel("Department: "),"cell 1 5");
		this.add(departmentbox,"cell 2 5");
		this.add(new JLabel("Title: "),"cell 4 5");
		this.add(titlefield,"cell 5 5");
		this.add(new JLabel("Salary: "),"cell 1 6");
		this.add(salaryfield,"cell 2 6");
		
		this.add(new JButton(new ButtonAction("Save")), "cell 4 6");
		this.add(new JButton(new ButtonAction("Cancel")), "cell 5 6");
		
		this.pack();
		//this.setModal(true);
		int w = getWidth();
		int h = getHeight();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);
	}
	
	private String validateFields(){
		String errors = "";
		if(namefield.getText().equals("") || !namefield.getText().contains(",")){
			errors += "Name\n";
		}
		if(emailfield.getText().equals("") || !emailfield.getText().contains("@")){
			errors += "Email\n";
		}
		if(ssnfield.getText().equals("")){
			errors += "SSN\n";
		}
		if(phoneNumberfield.getText().equals("")){
			errors += "Phone number\n";
		}
		if(addressfield.getText().equals("")){
			errors += "Address\n";
		}
		if(dobfield.getText().equals("")){
			errors += "D.O.B.\n";
		}
		if(emergencyContactfield.getText().equals("")){
			errors += "Emergency contact\n";
		}
		if(salaryfield.getText().equals("")){
			errors += "Salary\n";
		}
		else{
			try{
				float s = Float.valueOf(salaryfield.getText());
			}
			catch(Exception e){
				errors += "Salary\n";
			}
		}
		if(titlefield.getText().equals("")){
			errors += "Title\n";
		}
		
		return errors;
	}
	
	private class ButtonAction extends AbstractAction { 

		private static final long serialVersionUID = 1L;

		public ButtonAction(String name) {
			putValue(Action.NAME, name); 
		}
		
		public void actionPerformed(ActionEvent event) { 
			String name = (String)getValue(Action.NAME);

			if(name.equals("Save")){
				String errors = validateFields();
				if(errors.equals("")){
					emp.setName(namefield.getText());
					emp.setAddress(addressfield.getText());
					emp.setDateOfBirth(dobfield.getText());
					emp.setEmail(emailfield.getText());
					emp.setDepartment((String)departmentbox.getSelectedItem());
					emp.setEmergencyContact(emergencyContactfield.getText());
					emp.setGender((String)genderbox.getSelectedItem());
					emp.setPhoneNumber(phoneNumberfield.getText());
					emp.setSSN(ssnfield.getText());
					emp.setSalary(Float.valueOf(salaryfield.getText()));
					emp.setTitle(titlefield.getText());
					EmployeeDialog.this.setVisible(false);
					
					parent.addEmployee(emp);
				}
				else{
					JOptionPane.showMessageDialog(EmployeeDialog.this, "The following fields are invalid:\n" + errors,"Error", 0);
				}
			}
			else if(name.equals("Cancel")){
				EmployeeDialog.this.setVisible(false);
			}
		}
	}

}
